package com.intramirror.product.controller.price;

import com.intramirror.core.common.response.Response;
import com.intramirror.product.mapper.ProductDiscountMapper;
import com.intramirror.product.schedule.BurstingManager;
import com.intramirror.product.service.IDiscountService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/price/discount")
public class ProductDiscountByJobController {

    private static final Logger logger = LoggerFactory.getLogger(ProductDiscountByJobController.class);

    @Autowired
    private IDiscountService iDiscountService;

    @Autowired
    private ProductDiscountMapper productDiscountMapper;

    @Autowired
    private BurstingManager burstingManager;

    /**
     * 根据productId修改seasonCode并且刷新折扣
     */
    @PostMapping("/season")
    public Response updateProductSeasonAndDiscountById(@RequestBody Map<String, Object> productMap) {
        Long productId = Long.parseLong(productMap.get("productId").toString());
        String seasonCode = productMap.get("seasonCode").toString();

        iDiscountService.updateProductSeasonAndDiscountById(productId, seasonCode);
        return Response.success();
    }

    /**
     * 根据productId修改price并且刷新折扣
     */
    @PostMapping("/price")
    public Response updateProductPriceAndDiscountById(@RequestBody Map<String, Object> productMap) {
        Long productId = Long.parseLong(productMap.get("productId").toString());
        BigDecimal retailPrice = new BigDecimal(productMap.get("retailPrice").toString());

        iDiscountService.updateProductPriceAndDiscountById(productId, retailPrice);
        return Response.success();
    }

    /**
     * 根据productId修改折扣
     */
    @PostMapping("/product/{productId}")
    public Response updateProductDiscountById(@PathVariable("productId") Long productId) {
        iDiscountService.updateProductDiscountById(productId);
        return Response.success();
    }

    /**
     * 根据productId获取折扣
     */
    @GetMapping("/product/{productId}")
    public Response getProductDiscountById(@PathVariable("productId") Long productId) {
        Map<String, Object> data = iDiscountService.getProductDiscountById(productId);
        return Response.status(1).data(data);
    }

    /**
     * 根据折扣条件获取折扣
     */
    @PostMapping("/product")
    public Response getProductDiscount(@RequestBody Map<String, Object> productMap) {
        Map<String, Object> data = iDiscountService.getProductDiscountByMap(productMap);
        return Response.status(1).data(data);
    }

    @GetMapping("/getTodayDiscount")
    public Response getTodayDiscount() {
        return Response.status(1).data(productDiscountMapper.getTodayDiscount());
    }

    @GetMapping("/refresh/bursting")
    public Response refreshBursting() {
        burstingManager.refreshByDate(new Date());
        return Response.success();
    }

}
